<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
      <van-list v-model="loading" :finished="finished" finished-text="没有更多了" @load="onLoad">
        <div class="order-item" v-for="item in list" @click="goDetail(item)">
        <div class="order-row">
          <div class="order-el order-station"><img src="../assets/img/i4.png" height="20px"><!-- {{item.stationName}} -->水一汸</div>
          <div class="order-el order-status" v-bind:class="getOrderStatusClass(item)">
            {{getOrderStatus(item)}}
          </div>
        </div>
        <div class="order-row order-product">
          <!-- <div class="order-el order-img" v-bind:style="{backgroundImage: 'url(' + item.stationIcon + ')'}"></div> -->
          <div class="order-el order-info">
            <div class="order-info__item">
              下单时间：{{item.time}}
            </div>
            <div class="order-info__item">
              <template v-if="!item.barrel">
                实付金额：{{item.asum || 0}}元
              </template>
              <template v-else>
                应付金额：{{item.psum || 0}}元
              </template>
            </div>
            <div class="order-info__item order-info__item_paytype">
              支付方式：<span :style="{color: item.paytype ? '#f3d921' : 'rgb(38, 162, 255)'}">{{item.paytype ? '线下支付' : '线上支付'}}</span>
            </div>
          </div>
        </div>
        <div class="order-row order-sender-content">
          <div class="order-el" v-if="item.wState == 1">
            <van-button size="small" plain type="info" class="order-confirm" @click.stop="confirmSended(item)">确认送达</van-button>
          </div>
          <!-- <div class="order-el" v-if="!item.payStatus && item.state != 3">
            <van-button size="small" plain class="order-confirm" @click.stop="moreOrder(item)">再来一单</van-button>
          </div> -->
          <!-- <div class="order-el" v-if="item.state != 3 && !item.paytype">
            <van-button size="small" plain type="warning" class="order-confirm" @click.stop="payOrder(item)">付款</van-button>
          </div> -->
          <div class="order-el" v-if="item.payStatus && item.wState == 0">
            <van-button size="small" plain type="danger" class="order-confirm" @click.stop="cancel(item)">取消订单</van-button>
          </div>
        </div>
      </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
  import {
    ORDER_LIST,
    ORDER_DETAIL,
    ORDER_DEL,
    ORDER_CONFIRM
  } from '@/api/stock';
  import {
    mapState
  } from 'vuex'
  export default {
    props: {
      //状态查询参数
      state: {
        type: Number
      }
    },
    computed: {
      ...mapState(['stationInfo'])
    },
    data() {
      return {
        isLoading: false,
        finished: false,
        loading: false,
        currPage: 0,
        pageSize: 5,
        list: [],
        num: 4
      };
    },
    mounted() {
      // this.getList();
    },
    methods: {
      //获取列表
      getList() {
        if (this.stationInfo.id) {
          let params = {
            page: this.currPage,
            rows: this.pageSize,
            stationId: this.stationInfo.id
          }
          if(this.state !== undefined){
            params.state = this.state;
          }
          this.$reqPost(ORDER_LIST, params).then(res => {
            this.isLoading = false;
            this.loading = false;
            if(res.total == this.list.length){
              this.finished = true;
            }else{
              this.list.push(...res.rows);
            }
          });
        } else {
          this.$toast('请求失败');
        }
      },
      //获取详情
      goDetail(item) {
        this.$router.push({
          path: '/stockOrderDetail',
          query: {
            id: item.id
          }
        })
      },
      //刷新
      onRefresh() {
        this.list = [];
        this.currPage = 1;
        this.isLoading = true;
        this.finished = false;
        this.getList();
      },
      onLoad() {
        this.loading = true;
        this.currPage++;
        this.getList();
      },
      //付款
      payOrder(item){
        if((new Date().getTime() - new Date(item.time.replace(/\-/g, '/')).getTime()) > 30*60*1000){
          this.$toast('该订单已超时，请重新下单');
          this.cancel(item);
          return;
        }

        this.$router.push({
          name: 'stockPay',
          params: {
            orderNo: item.id,
            prepayId: item.prepayId,
            total: item.asum,
          },
          query: {
            time: new Date(item.time.replace(/\-/g, '/')).getTime()
          }
        });
      },
      //取消订单
      cancel(item){
        this.$reqGet(ORDER_DEL, {orderId: item.id, state: 3}).then(res => {
          this.$toast.success('取消成功');
          this.onRefresh();
        }).catch(err => {
          this.$toast.fail('取消失败');
        });
      },
      //确认送达
      confirmSended(item){
        this.$reqGet(ORDER_CONFIRM, {orderId: item.id,state: 2}).then(res => {
          this.$toast('提交成功');
          this.onRefresh();
        }).catch(err => {
          this.$toast.fail('提交失败');
        });
      },
      //获取工单状态
      getOrderStatus(item){
        if (item.wState == 3) {
          return '已取消';
        }
        if (!item.paytype) {
          if (item.payStatus) {
            return '待支付'
          }
        }
        if (item.wState == 0) {
          return '待配送';
        } else if (item.wState == 1) {
          return '配送中';
        } else if (item.wState == 2) {
          if(item.reOrder == 0){
            return '待审核';
          } else if(item.reOrder == 1){
            return '审核通过';
          } else if(item.reOrder == 2){
            return '不通过';
          }
        }
        return '';
      },
      getOrderStatusClass(item){
        if(item.wState == 3){
          return 'order-info__item__status__cancel';
        }
        if (!item.paytype) {
          if(item.payStatus){
            return 'order-info__item__status__unpay'
          }
        }
        if (item.wState == 0) {
          return 'order-info__item__status__wait';
        } else if (item.wState == 1) {
          return 'order-info__item__status__sending';
        } else if (item.wState == 2) {
          if(item.evaluation){
            return 'order-info__item__status__finish'
          }else{
            return 'order-info__item__status__finish';
          }
        }
        return '';
      }
    }
  };

</script>

<style lang="less" scoped>
  @import '../styles/variable.less';
  .order-item {
    margin: 0 10px 10px 10px;
    padding: 15px 15px 5px 15px;
    // border-bottom: 1px solid #dddddd;
    font-size: 14px;
    color: #666666;
    background-color: #ffffff;
    border-radius: 8px;

    &:first-child {
      margin-top: 10px;
    }
  }

  .order-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 7px;

    &.order-sender-content {
      justify-content: flex-end;
      margin-top: 5px;
      button{
        margin-left: 5px; 
      }
    }

    &.order-product {
      padding: 5px 0;
      justify-content: unset;
    }
  }

  .order-img {
    flex: 0 0 50px;
    height: 50px;
    margin-right: 10px;
    border-radius: 5px;
    background-repeat: no-repeat;
    background-position: center;
    background-size: contain;
    background-image: url('../assets/product.png');
  }

  .order-info {
    display: flex;
    flex-direction: column;
    flex: 1;

    .order-info__item {
      display: flex;
      justify-content: space-between;
      margin-bottom: 4px;
      color: #999999;
      &.order-info__item_paytype{
        justify-content: unset;
      }
      .order-info__item__name {
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }

    .order-info__more {
      color: #999999;
      text-align: right;
    }
  }



  //订单状态
  .order-status {
    &.order-status_cancel {
      color: #999999;
    }
    &.order-status_unpay {
      color: @col-red;
    }
    &.order-status_wait {
      color: @col-yellow;
    }

    &.order-status_sending {
      color: @col-the;
    }
  }

  //订单元素
  .order-el {
    &.order-station {
      color: #333;
      font-size: 16px;

      img {
        position: relative;
        top: 3px;
        margin-right: 10px;
      }
    }

    &.order-total {
      color: @col-the;
    }

    &.order-sender {
      display: flex;
      align-items: center;
    }

    .order-confirm {
      height: 26px;
      line-height: 24px;
      font-size: 12px;
    }

    .order-confirm::after {
      background-color: #dddddd;
    }
  }

</style>
